package com.stars.services.impl.system;
/**
 * Created by luodajie on 2019/1/5.
 */

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.stars.common.MyDateUtils;
import com.stars.common.base.BaseConstant;
import com.stars.common.result.VueTreeItem;
import com.stars.dao.mapper.system.SystemPermissionMapper;
import com.stars.dao.mapper.system.SystemUserMapper;
import com.stars.entity.ListPage;
import com.stars.entity.dto.sys.SystemPermissionDTO;
import com.stars.entity.dto.sys.SystemUserAppOrgDTO;
import com.stars.entity.dto.system.SysRoleDTO;
import com.stars.entity.sys.SystemPermission;
import com.stars.services.impl.BaseAccessServiceImpl;
import com.stars.services.system.PermissionAccessService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author :dj
 * @version : 1.0.0
 * @description
 * @Date 2019-01-05 12:19
 * @note
 */
@Service
public class PermissionAccessServiceImpl extends BaseAccessServiceImpl<SystemPermissionMapper, SystemPermission> implements PermissionAccessService {

    @Autowired
    private SystemPermissionMapper systemPermissionMapper;
    @Autowired
    private SystemUserMapper systemUserMapper;

    @Override
    public SystemPermission findByEntityId(String id) throws Exception {
        if (StringUtils.isNotBlank(id)) {
            SystemPermission systemPermission = systemPermissionMapper.selectByPrimaryKey(id);
            return systemPermission;
        }
        return null;
    }

    @Override
    public PageInfo<SystemPermissionDTO> findByPage(Integer pageNum, Integer pageSize, SystemPermissionDTO command) throws Exception {
        ListPage<SystemPermissionDTO> listPage = new ListPage<>(pageNum, pageSize, command);
        PageHelper.startPage(pageNum, pageSize);
        List<SystemPermissionDTO> blogList = systemPermissionMapper.findByPage(listPage);
        PageInfo<SystemPermissionDTO> pageInfo = new PageInfo<>(blogList);
        return pageInfo;
    }

    @Override
    public List<VueTreeItem> findByAll() throws Exception {
        List<SystemPermissionDTO> list = systemPermissionMapper.findByPerType("menu");
        List<VueTreeItem> vueTreeItemList = treeList(list);
        System.out.println("=========================="+ MyDateUtils.timestampe());
        return vueTreeItemList;
    }
    private List<VueTreeItem> treeList(List<SystemPermissionDTO> list) {
        List<VueTreeItem> vueTreeItemChildList = new ArrayList<>();

        int i = 0;
        for (SystemPermissionDTO child : list) {
            VueTreeItem vueTreeItemChild = new VueTreeItem();
            vueTreeItemChild.setId(child.getId());
            if ("1".equals(child.getPerEnabled())) {
                vueTreeItemChild.setDisabled(false);
            }
            vueTreeItemChild.setTitle(child.getPerName());
            vueTreeItemChild.setPid(child.getPid());
            List<SystemPermissionDTO> childList = child.getChildren();
            if (null != childList && childList.size() > 0) {
                List<VueTreeItem> vueTreeItemChildList1 = treeList(childList);
                vueTreeItemChild.setChildren(vueTreeItemChildList1);
            }
            if (i >= 1) {
                vueTreeItemChild.setExpand(false);
            }
            vueTreeItemChildList.add(vueTreeItemChild);
            i++;
        }
        return vueTreeItemChildList;
    }



    @Override
    public List<SystemPermissionDTO> findByPid(String parentId) throws Exception {
        if (StringUtils.isNotBlank(parentId)) {
            return systemPermissionMapper.findChildMenusByPid(parentId);
        }
        return null;
    }

    @Override
    public List<SystemPermissionDTO> findPermissionByPerType(String perType) throws Exception {
        if (StringUtils.isNotBlank(perType)) {
            return systemPermissionMapper.findByPerType(perType);
        }
        return null;
    }
}
